package com.xianggu.spring.expand;

import com.xianggu.spring.BeanDefinition;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExtensionRegistry {
    private static List<BeanLoadingExtension> extensions = new ArrayList<>();

    public static void registerExtension(BeanLoadingExtension extension) {
        extensions.add(extension);
    }
    public static void registerExtension(List<BeanLoadingExtension> beans) {
        extensions.addAll(beans);
    }

    /**
     * 拓展创建类
     *
     * @param beanName
     * @param beanDefinition
     * @return
     */
    public static Object executeCreateBean(String beanName, BeanDefinition beanDefinition) {
        for (BeanLoadingExtension extension : extensions) {
            // 执行拓展bean逻辑
            Object result = extension.createBean(beanName, beanDefinition);
            if (result != null) {
                return result;
            }
        }
        return null;
    }


    /**
     * 条件检验
     */
    public static BeanLoadingExtension satisfyCondition(Class<?> clazz){
        return extensions.stream()
                .filter(extension -> extension.isBean(clazz)).findFirst().orElse(null);
    }
}